Skip to main content

Read a File in Python

To programmatically read data from a file using Python, it must be opened first. Use the built-in open() function:

file_object = open(file_name [, access_mode][, buffering])

Here are the parameter details:

  • file_name: The name of the file you want to access.
  • access_mode: Determines the mode in which the file is opened, e.g., read, write, append (optional, default is read mode).
  • buffering: Specifies buffering options (optional).

These two statements are identical:

fo = open("foo.txt", "r")
fo = open("foo.txt")

Reading Text Files​

To read data from the opened file, use the read() method of the File object:

file_object.read([count])

Here, count is the number of bytes to be read from the file (optional). If count is missing, it reads until the end of the file.

count βˆ’ Number of bytes to be read.

Example​

# Open a file
fo = open("foo.txt", "r")
text = fo.read()
print(text)

# Close the opened file
fo.close()

Reading Binary Files​

To handle files like media or executables, use binary mode by adding a 'b' prefix:

f = open('test.bin', 'wb')
data = b"Hello World"
f.write(data)
f.close()

Example​

f = open('test.bin', 'rb')
data = f.read()
print(data.decode(encoding='utf-8'))

Reading Numbers from a File​

Integer Data​

To write and read integer data in a binary file:

n = 25
n_bytes = n.to_bytes(8, 'big')
f = open('test.bin', 'wb')
f.write(n_bytes)

To read back from the file:

f = open('test.bin', 'rb')
data = f.read()
n = int.from_bytes(data, 'big')
print(n)

Float Data​

For floating point data, use the struct module:

import struct
x = 23.50
data = struct.pack('f', x)
f = open('test.bin', 'wb')
f.write(data)

Example​

f = open('test.bin', 'rb')
data = f.read()
x = struct.unpack('f', data)
print(x)

Reading Using Reading and Writing (r+) Mode​

To perform both read and write operations simultaneously:

fo = open("foo.txt", "r+")
fo.seek(10, 0)
data = fo.read(3)
print(data)
fo.close()

The File object also supports the seek() function to rewind the stream to read from any desired byte position.

Following is the syntax for seek() method βˆ’

fileObject.seek(offset[, whence])

Parameters​

  • offset βˆ’ This is the position of the read/write pointer within the file.

  • whence βˆ’ This is optional and defaults to 0 which means absolute file positioning, other values are 1 which means seek relative to the current position and 2 means seek relative to the file's end.

Simultaneous Read and Write​

For simultaneous read and write, use 'r+' or 'w+' mode:

fo = open("foo.txt", "r+")
fo.write("This is a rat race")
fo.seek(10, 0)
data = fo.read(3)
fo.seek(10, 0)
fo.write('cat')
fo.close()

Read a File from Specific Offset​

The seek() method sets the file's current position. Example:

Following is the syntax for seek() method βˆ’

fileObject.seek(offset[, whence])

Parameters​

  • offset βˆ’ This is the position of the read/write pointer within the file.

  • whence βˆ’ This is optional and defaults to 0 which means absolute file positioning, other values are 1 which means seek relative to the current position and 2 means seek relative to the file's end.

Examples​

fo = open("foo.txt", "r+")
fo.seek(10, 0)
data = fo.read()
print(data)
fo.close()

This program reads from a specific offset and prints the data.

This is a cat race